<?php
class Friends extends CI_Controller {
	
	/**
	 * @return CI_Controller
	 */
	public static function get_instance() {
		
	}
	
	function index() {
		if($this->session->userdata('is_logged_in'))
		{
			$this->load->model('friends_model');
			$result=$this->friends_model->get_friends();
			$data=array('friend_model'=>'list','friends'=>$result);
			$this->load->view('friends/main',$data);
			
		}
		else
			redirect(base_url());
	}
	
	function listview(){
		if($this->session->userdata('is_logged_in'))
		{
			$this->load->model('friends_model');
			$result=$this->friends_model->get_friends();
			$data=array('friend_model'=>'list','friends'=>$result);
			$this->load->view('friends/main',$data);
			
		}
		else
			redirect(base_url());
	}
	
	function find(){
		if($this->session->userdata('is_logged_in'))
		{
			$this->load->model('friends_model');
			$row=$this->friends_model->get_commend_user();
			$row_setting=$this->friends_model->get_commend_user_setting($row->user_id);
			$data=array('user'=>$row,'user_setting'=>$row_setting,'friend_model'=>'find');
			$this->load->view('friends/main',$data);			
		}
		else
			redirect(base_url());
	}
	
	function search(){
		if($this->session->userdata('is_logged_in'))
		{
			$data=array('friend_model'=>'search');
			$this->load->view('friends/main',$data);
		}
		else
			redirect(base_url());
	}
	
	function request(){
		if($this->session->userdata('is_logged_in'))
		{
			$this->load->model('friends_model');
			$this->load->model('friendlog_model');
			//获取是否有需要处理的申请
			$result=$this->friendlog_model->get_request_log();
			if(!$result){
				$data=array('request'=>false,'friend_model'=>'request');
				$this->load->view('friends/main',$data);
			}
			else
			{
				//分页的参数
				$this->load->library('pagination');
				$config['base_url'] = base_url().'index.php/friends/request';
				$config['total_rows'] = count($result);
				$config['per_page'] = '5';
				$config['full_tag_open'] = '<p>';
				$config['full_tag_close'] = '</p>';
				$this->pagination->initialize($config);
				
				//按照偏移来获取数据
				$results=$this->friendlog_model->get_request_log($config['per_page'],$this->uri->segment(3));
				
				$user_array=array();
				foreach($results as $row){
					$this->load->model('user_setting_model');
					$this->load->model('user_model');
					$setting=$this->user_setting_model->get_user_setting_info($row->user_id);
					$user=$this->user_model->get_user_info($row->user_id);
					
					$data=array(
						'photo_path'=>$setting->photo_path,
						'id'=>$user->user_id,
						'name'=>strlen($setting->n_name)==0 ? $user->user_name : $setting->n_name,
						'time'=>$row->dateline,
						'note'=>$row->note
					);
					array_push($user_array,$data);
				}
				$this->load->view('friends/main',array('request'=>$user_array,'friend_model'=>'request'));		
			}
		}
		else
			redirect(base_url());
	}
	
	function concept_friend(){
		if($this->session->userdata('is_logged_in'))
		{
			$id=$_GET['id'];
			$this->load->model('friendlog_model');
			$this->load->model('friends_model');
			$log_request=$this->friendlog_model->concept($id);
			$request=$this->friends_model->concept($id);
			
			if($log_request && $request)
				echo $id;
			else 
				echo false;	
		}
		else
			redirect(base_url());
		
	}
	
	function refuse_friend(){
		if($this->session->userdata('is_logged_in'))
		{
			$id=$_GET['id'];
			$this->load->model('friendlog_model');
			$this->load->model('friends_model');
			$log_request=$this->friendlog_model->refuse($id);
			$request=$this->friends_model->refuse($id);
			
			if($log_request && $request)
				echo $id;
			else 
				echo false;	
		}
		else
			redirect(base_url());
	}
	
	
	
}
?>